package com.adpter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.common.method.ChatHelper;
import com.common.method.ServerPath;
import com.contact.UserVAudio;
import com.contact.VideoPlayerUserActivity;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.matang.R;
import com.modle.ChatMsgStruct;

public class ChatMsgViewAdapter extends CusBaseAdapter {
	private BitmapUtils bitmapUtils;
	public static interface IMsgViewType {
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}

	private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();

	private List<ChatMsgStruct> coll;

	private Context ctx;

	private LayoutInflater mInflater;
	private MediaPlayer mMediaPlayer = new MediaPlayer();

	public ChatMsgViewAdapter(Context context, List<ChatMsgStruct> coll) {
		ctx = context;
		this.coll = coll;
		mInflater = LayoutInflater.from(context);
		if (bitmapUtils == null) {
			bitmapUtils = new BitmapUtils(context);
		}
	}

	public int getCount() {
		return coll.size();
	}

	public Object getItem(int position) {
		return coll.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		ChatMsgStruct entity = coll.get(position);

		if (entity.getMsgType()) {
			return IMsgViewType.IMVT_COM_MSG;
		} else {
			return IMsgViewType.IMVT_TO_MSG;
		}

	}

	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		final ChatMsgStruct entity = coll.get(position);
		boolean isComMsg = entity.getMsgType();

		ViewHolder viewHolder = null;
		if (convertView == null) {
			if (isComMsg) {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_left, null);
			} else {
				convertView = mInflater.inflate(
						R.layout.chatting_item_msg_text_right, null);
			}

			viewHolder = new ViewHolder();
			viewHolder.tvSendTime = (TextView) convertView
					.findViewById(R.id.tv_sendtime);
			viewHolder.tvUserName = (TextView) convertView
					.findViewById(R.id.tv_username);
			viewHolder.tvContent = (TextView) convertView
					.findViewById(R.id.tv_chatcontent);
			viewHolder.tvTime = (TextView) convertView
					.findViewById(R.id.tv_time);
			viewHolder.ivThumb=(ImageView)convertView.findViewById(R.id.iv_userhead);
			viewHolder.imgTp = (ImageView)convertView.findViewById(R.id.img_tp);
			viewHolder.isComMsg = isComMsg;

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.tvSendTime.setText(entity.getDate());
		viewHolder.tvUserName.setText(entity.getName());
		imageLoader.bind(viewHolder.ivThumb,ServerPath.SERVER+entity.getThumb(), null, R.drawable.default_head);
		if (entity.getText().contains(".3gp") || entity.getText().contains(".mp4")) {
			
			viewHolder.tvContent.setText("    视频     ");
			viewHolder.tvContent.setVisibility(View.VISIBLE);
			viewHolder.imgTp.setVisibility(View.GONE);
			// 在聊天的条目放个图片
			if (isComMsg) {
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatfrom_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.chatto_voice_playing_l, 0, 0, 0);

			} else {
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatto_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
						0, R.drawable.chatto_voice_playing_r, 0);
				// viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
				// 0, 0, 0);
			}
			if (entity.getTime() != null && !entity.getTime().trim().equals("")) {
				viewHolder.tvContent.setText(ChatHelper
						.getVoiceTextWidth(Integer.parseInt(entity.getTime())));
				viewHolder.tvTime.setText(entity.getTime() + "\"");
			}
		} else if (entity.getText().contains(".amr")) {
			viewHolder.tvContent.setVisibility(View.VISIBLE);
			viewHolder.imgTp.setVisibility(View.GONE);
			viewHolder.tvContent.setText("                             ");

			// 在聊天的条目放个图片
			if (isComMsg) {
				
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatfrom_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.chatto_voice_playing_l, 0, 0, 0);

			} else {
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatto_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
						0, R.drawable.chatto_voice_playing_r, 0);
				// viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
				// 0, 0, 0);
			}
			if (entity.getTime() != null && !entity.getTime().trim().equals("")) {
				viewHolder.tvContent.setText(ChatHelper
						.getVoiceTextWidth(Integer.parseInt(entity.getTime())));
				viewHolder.tvTime.setText(entity.getTime() + "\"");
			}
		} else if (entity.getText().contains(".jpg") || entity.getText().contains(".png")) {
			viewHolder.tvContent.setVisibility(View.VISIBLE);
			viewHolder.imgTp.setVisibility(View.VISIBLE);
			String text = entity.getText();
			String path = ServerPath.SERVER + text;
			bitmapUtils.display(viewHolder.tvContent, path, new BitmapLoadCallBack<View>() {

			
				public void onLoadCompleted(View container, String uri,
						Bitmap bitmap, BitmapDisplayConfig config,
						BitmapLoadFrom from) {
					Bitmap myBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);    
					((TextView)container).setBackgroundDrawable(new BitmapDrawable(myBitmap));
					
				}

				@Override
				public void onLoadFailed(View container, String uri,
						Drawable drawable) {
				
					
				}
			});
			//viewHolder.tvContent.setText("  图片   ");

			// 在聊天的条目放个图片
//			if (isComMsg) {
//				viewHolder.tvContent.setBackgroundResource(R.drawable.chatfrom_bg);
//				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(
//						0, 0, 0, 0);
//
//			} else {
//				viewHolder.tvContent.setBackgroundResource(R.drawable.chatto_bg);
//				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
//						0, 0, 0);
//				// viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
//				// 0, 0, 0);
//			}
			if (entity.getTime() != null && !entity.getTime().trim().equals("")) {
				viewHolder.tvContent.setText(ChatHelper
						.getVoiceTextWidth(Integer.parseInt(entity.getTime())));
				viewHolder.tvTime.setText(entity.getTime() + "\"");
			}
		}else if (entity.getText().contains("/") && entity.getText().contains(".")) {
			viewHolder.tvContent.setVisibility(View.VISIBLE);
			viewHolder.imgTp.setVisibility(View.VISIBLE);
			String name = entity.getText();
			String fileName = name.substring(name.lastIndexOf("/")+1);
			viewHolder.tvContent.setText(" 文件   "+fileName);
			viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(
					0, 0, 0, 0);

			// 在聊天的条目放个图片
			if (isComMsg) {
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatfrom_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(
						R.drawable.chatto_voice_playing_l, 0, 0, 0);

			} else {
				viewHolder.tvContent.setBackgroundResource(R.drawable.chatto_bg);
				viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
						0, R.drawable.chatto_voice_playing_r, 0);
				// viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0,
				// 0, 0, 0);
			}
			if (entity.getTime() != null && !entity.getTime().trim().equals("")) {
				viewHolder.tvContent.setText(ChatHelper
						.getVoiceTextWidth(Integer.parseInt(entity.getTime())));
				viewHolder.tvTime.setText(entity.getTime() + "\"");
			}
		}
		
		else
		{	viewHolder.imgTp.setVisibility(View.GONE);
			viewHolder.tvContent.setText(entity.getText());
			viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					0, 0);
			viewHolder.tvTime.setText("");
		}
		
		
	
		viewHolder.tvContent.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String text = entity.getText();
				if (text.contains(".amr")) {
					playMusic(ChatHelper.getVoiceSavePath()
							+ text.substring(text.lastIndexOf("/") + 1));
				}else if(text.contains(".3gp") || text.contains(".mp4")){
					
					Intent intent = new Intent();
					intent.setClass(ctx,
							VideoPlayerUserActivity.class);
					UserVAudio userVAudio = new UserVAudio();
					userVAudio.videoUrl = ServerPath.SERVER + text;
					intent.putExtra("uvData", userVAudio);
					ctx.startActivity(intent);
					
				}else if(text.contains(".jpg") || text.contains(".png")){
					String path = ServerPath.SERVER + text;
					showImage(path);
					
				}else if(text.contains("/") && text.contains(".")){
				Toast.makeText(ctx, "无法打开此类型的文件", 1).show();
					
				}
			}
		});
		

		return convertView;
	}
	
	private void showImage(String uri){
		View view = LayoutInflater.from(ctx).inflate(R.layout.show_img, null);		 
		ImageView imageView = (ImageView) view.findViewById(R.id.img_show);
		bitmapUtils.display(imageView, uri);
		AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
		alertDialog.setView(view);
		alertDialog.show();
	}

	class ViewHolder {
		public ImageView imgTp;
		public TextView tvSendTime;
		public TextView tvUserName;
		public TextView tvContent;
		public TextView tvTime;
		public ImageView ivThumb;
		public boolean isComMsg = true;
	}

	/**
	 * @Description
	 * @param name
	 */
	private void playMusic(String name) {
		try {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(name);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void stop() {

	}

}
